﻿<%@ WebHandler Language="C#" Class="GetProjectData" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Script.Serialization;

public class GetProjectData : IHttpHandler
{
    private HsTec.Mobile.Green.BLL.TotalProjectBLL bll = new HsTec.Mobile.Green.BLL.TotalProjectBLL();
    private HsTec.Mobile.Green.BLL.SingleProjectBLL sbll = new HsTec.Mobile.Green.BLL.SingleProjectBLL();

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string sId = context.Request.Form["id"];
        string sState = context.Request.Form["checkstate"];
        if (String.IsNullOrEmpty(sId) || sId == "0")
        {
            TreeItem tdata = new TreeItem() { id = "0", checkstate = 0, complete = true, isexpand = true, showcheck = true, text = "工程项目", value = "TotalProject_Add.aspx?t=0", hasChildren = true };
            tdata.ChildNodes = new List<TreeItem>();
            List<HsTec.Mobile.Green.Model.TotalProject> listRoot = bll.GetModelList("");
            foreach (HsTec.Mobile.Green.Model.TotalProject rt in listRoot)
            {
                TreeItem ti0 = new TreeItem();
                ti0.id = rt.tp_id.ToString();
                ti0.value = "TotalProject_Add.aspx?t=1&id=" + rt.tp_id.ToString();
                ti0.text = rt.name;
                ti0.isexpand = false;
                ti0.hasChildren = true;
                ti0.complete = false;
                tdata.ChildNodes.Add(ti0);
            }
            context.Response.Write(tdata.ToJsonString());
        }
        else
        {
            List<TreeItem> list = new List<TreeItem>();
            List<HsTec.Mobile.Green.Model.SingleProject> listNode = sbll.GetModelList("tp_id=" + sId);
            foreach (HsTec.Mobile.Green.Model.SingleProject rt in listNode)
            {
                TreeItem ti0 = new TreeItem();
                ti0.id = rt.sp_id.ToString();
                ti0.value = "SingleProject_Add.aspx?t=1&p=" + sId + "&id=" + rt.sp_id.ToString();
                ti0.text = rt.spname;
                ti0.isexpand = false;
                ti0.hasChildren = false;
                ti0.complete = false;
                list.Add(ti0);
            }
            context.Response.Write(this.ListToJson(list));
        }
    }

    private string ListToJson(List<TreeItem> list)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        string sJson = String.Empty;
        foreach (TreeItem ti in list)
        {
            if (String.IsNullOrEmpty(sJson))
                sJson = jss.Serialize(ti);
            else
                sJson += "," + jss.Serialize(ti);
        }
        return "[" + sJson + "]";
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}